package Download;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import DAO.SongDOA;
import DAO.Song_ListDAO;
import each_class.Album;
import each_class.RanKingClass;
import each_class.SongClass;
import each_class.User;
import DAO.Singers_DAO;

public class LoadUser {

	public static User loadUser(Map<String, Object> map) { // 加载用户
		User us = new User();
		if (map != null) {
			for (Entry<String, Object> entry : map.entrySet()) {
				// System.out.println(entry.getKey() +" " +entry.getValue());
				switch (entry.getKey()) {
				case "USER_ID":
					us.setUser_id((String) entry.getValue());
					// System.out.println(us.getUser_id());
					break;
				case "UNAME":
					us.setUname((String) entry.getValue());
					break;
				case "UPASSWORD":
					us.setUpassword((String) entry.getValue());
					break;
				case "USER_CACHINGPATH":
					us.setUser_cachingpath((String) entry.getValue());
					break;
				case "USER_DOWPATH":
					us.setUser_dowpath((String) entry.getValue());
					break;
				case "USER_IMAGE":
					byte[] bs = (byte[]) entry.getValue();
					InputStream inputStream = new ByteArrayInputStream(bs);
					us.setUser_imagepath(inputStream);
					break;
				}

			}
			return us;
		}
		return null;
	}

	public static SongClass loadSong(Map<String, Object> map) throws IOException { // 加载歌曲
		SongClass sc = new SongClass();
		if (map != null) {
			for (Entry<String, Object> entry : map.entrySet()) {
				switch (entry.getKey()) {
				case "SONG_ID":
					sc.setSong_id((String) entry.getValue());
					break;
				case "SONG_NAME":
					sc.setSong_name((String) entry.getValue());
					break;
				case "SINGER_ID":
					Map<String, String> sinmap = new HashMap<String, String>();
					String sid = (String) entry.getValue();
					String[] ids = sid.split("/");
					Singers_DAO sind = new Singers_DAO();
					for (int i = 0; i < ids.length; i++) {
						sinmap.put((String) sind.find(ids[i]).get("singer_id"),
								(String) sind.find(ids[i]).get("singer_name"));
					}
					break;
				case "SONG_ALBUM_ID":
					sc.setAlbum_id((String) entry.getValue());
					break;
				case "SONG_ALBUM_NAME":
					sc.setAlbum_name((String) entry.getValue());
					break;
				case "SONG_TIME":
					sc.setSong_time((String) entry.getValue());
					break;
				case "IMAGE_URL":
					sc.setSong_image_url((String) entry.getValue());
					break;
				case "SONG_CACHINGPATH":
					sc.setCachingpath((String) entry.getValue());
					if ((String) entry.getValue() != null) {
						sc.setFavorite(true);
					}
					break;
				case "SONG_DOWPATH":
					sc.setDowpath((String) entry.getValue());
					if ((String) entry.getValue() != null) {
						sc.setDownload(true);
					}
					break;
				}

			}

			try {
				sc.setLyric(FindMusic.findlyric(sc));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// System.out.println(sc.toString());
			return sc;
		}
		return null;
	}

	public static RanKingClass loadSongList(List<Map<String, Object>> lmap) { // 加载歌单
		RanKingClass rkc = new RanKingClass();
		if (lmap != null) {
			for (Map<String, Object> map : lmap) {
				for (Entry<String, Object> entry : map.entrySet()) {
					switch (entry.getKey()) {
					case "LIST_ID":
						rkc.setId((String) entry.getValue());
						break;
					case "LIST_NAME":
						rkc.setRanKingClass_name((String) entry.getValue());
						break;
					case "LIST_IMAGEURL":
						rkc.setRanKing_image_url((String) entry.getValue());
						break;
					}
				}
			}
			return rkc;
		}
		return null;
	}

	public static RanKingClass loadSongList(Map<String, Object> map) { // 加载歌单
		RanKingClass rkc = new RanKingClass();
		if (map != null) {
			for (Entry<String, Object> entry : map.entrySet()) {
				switch (entry.getKey()) {
				case "LIST_ID":
					rkc.setId((String) entry.getValue());
					break;
				case "LIST_NAME":
					rkc.setRanKingClass_name((String) entry.getValue());
					break;
				case "LIST_IMAGEURL":
					rkc.setRanKing_image_url((String) entry.getValue());
					break;
				}
			}
			return rkc;
		}
		return null;
	}

	public static List<?> loadILike(List<Map<String, Object>> lmap, User us) throws IOException { // 加载我喜欢
		List<SongClass> songClasses = new ArrayList<SongClass>();
		List<RanKingClass> rkc = new ArrayList<RanKingClass>();
		List<Album> albums = new ArrayList<Album>();
		if (lmap != null) {
			for (Map<String, Object> map : lmap) {
				for (Entry<String, Object> entry : map.entrySet()) {
					switch (entry.getKey()) {
					case "SONG_ID":
						songClasses.add(loadSong(SongDOA.find((String) entry.getValue())));
						break;
					case "LIST_ID":
						rkc.add(loadSongList(Song_ListDAO.find(us.getUser_id(), (String) entry.getValue())));
						break;
					case "ALBUM_ID":
						albums.add(loadAlbum(Song_ListDAO.find(us.getUser_id(), (String) entry.getValue())));
						break;
					}
				}
			}
			if (songClasses.size() != 0) {
				return songClasses;
			} else if (rkc.size() != 0) {
				return rkc;
			} else {
				return albums;
			}

		}
		return null;
	}

	public static Album loadAlbum(Map<String, Object> map) { // 加载专辑
		Album album = new Album();
		if (map != null) {
			for (Entry<String, Object> entry : map.entrySet()) {
				switch (entry.getKey()) {
				case "LIST_ID":
					album.setAlbm_id((String) entry.getValue());
					break;
				case "LIST_NAME":
					album.setAlbm_name((String) entry.getValue());
					break;
				case "LIST_IMAGEURL":
					album.setAlbm_image_url((String) entry.getValue());
					break;
				}
			}
			return album;
		}
		return null;
	}

	public static List<SongClass> loadAlbumSongs(User us, String listid) throws IOException {

		Album album = loadAlbum(Song_ListDAO.find(us.getUser_id(), listid));

		List<Map<String, Object>> lmap = Song_ListDAO.findSongAll(us.getUser_id(), album.getAlbm_id());
		List<SongClass> list = new ArrayList<SongClass>();
		for (Map<String, Object> map : lmap) {
			for (Entry<String, Object> entry : map.entrySet()) {
				switch (entry.getKey()) {
				case "SONG_ID":
					list.add(loadSong(SongDOA.find((String) entry.getValue())));
					break;
				}
			}
		}
		album.setsClasses(list);
		return list;
	}

	public static List<SongClass> loadRKCSongs(User us, String listid) throws IOException {

		RanKingClass rkc = loadSongList(Song_ListDAO.find(us.getUser_id(), listid));

		List<Map<String, Object>> lmap = Song_ListDAO.findSongAll(us.getUser_id(), rkc.getId());
		List<SongClass> list = new ArrayList<SongClass>();
		for (Map<String, Object> map : lmap) {
			for (Entry<String, Object> entry : map.entrySet()) {
				switch (entry.getKey()) {
				case "SONG_ID":
					list.add(loadSong(SongDOA.find((String) entry.getValue())));
					break;
				}
			}
		}
		rkc.setSongList(list);
		return list;
	}

}
